package com.yanqu.road.dao.impl.player;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.zhenhuan.firstProcess.UserZhenHuanFirstProcessData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UserZhenHuanFirstDaoImpl extends TempDataObjectDao<UserZhenHuanFirstProcessData> {
    public static String getUserZhenHuanFirstProcessDataSql = "select * from `t_u_zhenhuan_first` where `activityId`=? and `userId`=?";
    public UserZhenHuanFirstProcessData getUserZhenHuanFirstProcessData(int activityId,long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getUserZhenHuanFirstProcessDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateUserZhenHuanFirstProcessDataSql = "update `t_u_zhenhuan_first` set power=?,powerRecoverTime=?,score=?,round=?,storyId=?,choseMap=?,unlockStory=?,isWorship=?,specialAmount=? where activityId=? and userId=?";
    @Override
    protected boolean updateData(UserZhenHuanFirstProcessData userZhenHuanFirstProcessData) {
        userZhenHuanFirstProcessData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, userZhenHuanFirstProcessData.getPower()));
        para.put(i++, new DbParameter(Types.BIGINT, userZhenHuanFirstProcessData.getPowerRecoverTime()));
        para.put(i++, new DbParameter(Types.INTEGER, userZhenHuanFirstProcessData.getScore()));
        para.put(i++, new DbParameter(Types.INTEGER, userZhenHuanFirstProcessData.getRound()));
        para.put(i++, new DbParameter(Types.INTEGER, userZhenHuanFirstProcessData.getStoryId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userZhenHuanFirstProcessData.getChoseMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userZhenHuanFirstProcessData.getUnlockStory())));
        para.put(i++, new DbParameter(Types.BOOLEAN, userZhenHuanFirstProcessData.isWorship()));
        para.put(i++, new DbParameter(Types.INTEGER, userZhenHuanFirstProcessData.getSpecialAmount()));
        para.put(i++, new DbParameter(Types.INTEGER, userZhenHuanFirstProcessData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, userZhenHuanFirstProcessData.getUserId()));
        if (getDb().executeNoneQuery(updateUserZhenHuanFirstProcessDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertUserZhenHuanFirstProcessDataSql = "insert into `t_u_zhenhuan_first`(activityId,userId,power,powerRecoverTime,score,round,storyId,choseMap,unlockStory,isWorship,specialAmount) values (?,?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(UserZhenHuanFirstProcessData userZhenHuanFirstProcessData) {
        userZhenHuanFirstProcessData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, userZhenHuanFirstProcessData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, userZhenHuanFirstProcessData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, userZhenHuanFirstProcessData.getPower()));
        para.put(i++, new DbParameter(Types.BIGINT, userZhenHuanFirstProcessData.getPowerRecoverTime()));
        para.put(i++, new DbParameter(Types.INTEGER, userZhenHuanFirstProcessData.getScore()));
        para.put(i++, new DbParameter(Types.INTEGER, userZhenHuanFirstProcessData.getRound()));
        para.put(i++, new DbParameter(Types.INTEGER, userZhenHuanFirstProcessData.getStoryId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userZhenHuanFirstProcessData.getChoseMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userZhenHuanFirstProcessData.getUnlockStory())));
        para.put(i++, new DbParameter(Types.BOOLEAN, userZhenHuanFirstProcessData.isWorship()));
        para.put(i++, new DbParameter(Types.INTEGER, userZhenHuanFirstProcessData.getSpecialAmount()));
        if (getDb().executeNoneQuery(insertUserZhenHuanFirstProcessDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected UserZhenHuanFirstProcessData parseData(ResultSet rs) throws SQLException {
        UserZhenHuanFirstProcessData userZhenHuanFirstProcessData = new UserZhenHuanFirstProcessData();
        userZhenHuanFirstProcessData.setActivityId(rs.getInt("activityId"));
        userZhenHuanFirstProcessData.setUserId(rs.getLong("userId"));
        userZhenHuanFirstProcessData.setPower(rs.getInt("power"));
        userZhenHuanFirstProcessData.setPowerRecoverTime(rs.getLong("powerRecoverTime"));
        userZhenHuanFirstProcessData.setScore(rs.getInt("score"));
        userZhenHuanFirstProcessData.setRound(rs.getInt("round"));
        userZhenHuanFirstProcessData.setStoryId(rs.getInt("storyId"));
        userZhenHuanFirstProcessData.setChoseMap(JSON.parseObject(rs.getString("choseMap"), new TypeReference<java.util.Map<java.lang.Integer, java.lang.Integer>>() {}));
        userZhenHuanFirstProcessData.setUnlockStory(JSON.parseObject(rs.getString("unlockStory"), new TypeReference<java.util.Map<java.lang.Integer, java.lang.Integer>>() {}));
        userZhenHuanFirstProcessData.setWorship(rs.getBoolean("isWorship"));
        userZhenHuanFirstProcessData.setSpecialAmount(rs.getInt("specialAmount"));

        return userZhenHuanFirstProcessData;
    }
}
